import Vue from 'vue'
import {
  ACCESS_TOKEN
} from "@/store/mutation-types"
import store from '@/store'
import * as dd from 'dingtalk-jsapi'

/**
 * 单点登录
 */

const init = (callback) => {
  console.log(window.location.pathname)

  if (dd.env.platform != 'notInDingTalk') {

    let pathname = window.location.pathname;
    let pathArr = pathname.split('/')
    if (pathArr.indexOf('white') > -1) {
      if (process.env.NODE_ENV === 'test') {
        if (pathArr[2] == 'white') {
          console.log("------是白名单！！！-------");
          callback()
          return
        }
      } else {
        if (pathArr[1] == 'white') {
          callback();
          return
        }
      }
    }
    dd.ready(() => {

      dd.device.base.getPhoneInfo({

        onSuccess: function (data) {

          /*
          {
              screenWidth: 1080, // 手机屏幕宽度
              screenHeight: 1920, // 手机屏幕高度
              brand:'Mi', // 手机品牌
              model:'Note4', // 手机型号
              version:'7.0', // 版本
              netInfo:'wifi', // 网络类型 wifi／4g／3g 
              operatorType:'xx' // 运营商信息
          }
          */
          // alert(JSON.stringify(data));

          window.location.href = 'https://img.bzhz.jnbygroup.com/abnormal.html'
          return;

        },
        onFail: function (err) {

        }
      });

      dd.runtime.permission.requestAuthCode({
        corpId: 'ding8bac6beff1d2d841'
      }).then((result) => {
        dd.biz.util.openLink({
          url: "https://retail.jnby.com", //要打开链接的地址
          onSuccess: function (result) {
            /**/
          },
          onFail: function (err) { }
        })


      }).catch(err => {
        console.log(err);
        alert(err);
      });
    });


    return;
  }

  let pathname = window.location.pathname;
  let pathArr = pathname.split('/')
  if (pathArr.indexOf('white') > -1) {
    if (process.env.NODE_ENV === 'test') {
      if (pathArr[2] == 'white') {
        console.log("------是白名单！！！-------");
        callback()
      }
    } else {
      if (pathArr[1] == 'white') {
        console.log("------是白名单！！！-------");
        callback()
      }
    }
  } else {
    if (process.env.VUE_APP_SSO == 'true') {
      console.log("-------单点登录开始-------");
      let token = Vue.ls.get(ACCESS_TOKEN);
      let st = getUrlParam("ticket");
      let sevice = window.location.href;
      let that = this;
      if (token) {
        loginSuccess(callback);
      } else {
        if (st) {
          validateSt(st, sevice, callback);
        } else {
          let serviceUrl = encodeURIComponent(sevice);
          window.location.href = window._CONFIG['casPrefixUrl'] + "/login?service=" + serviceUrl;
        }
      }
      console.log("-------单点登录结束-------");
    } else {
      callback && callback()
    }
  }

};
const SSO = {
  init: init
};

function getUrlParam (paraName) {
  let url = document.location.toString();
  let arrObj = url.split("?");

  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split("&");
    let arr;

    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");

      if (arr != null && arr[0] == paraName) {
        return arr[1];
      }
    }
    return "";
  } else {
    return "";
  }
}

function validateSt (ticket, service, callback) {
  var ser = service.split('?')[0]
  let params = {
    ticket: ticket,
    service: ser
  };
  store.dispatch('ValidateLogin', params).then(res => {
    //this.departConfirm(res)
    if (res.success) {
      loginSuccess(callback);
    } else {
      // let sevice = window.location.href
      let sevice = window.location.href.split('?')[0];
      let serviceUrl = encodeURIComponent(sevice);
      console.log(sevice + ';' + serviceUrl)
      window.location.href = window._CONFIG['casPrefixUrl'] + "/login?service=" + serviceUrl;
    }
  }).catch((err) => {
    console.log(err);
    //that.requestFailed(err);
  });
}

function loginSuccess (callback) {
  callback();
}
export default SSO;